package com.zcy.gift.rocketmq.listener;

import com.alibaba.fastjson.JSONArray;
import com.zcy.gift.constant.GiftConstant;
import com.zcy.gift.doc.GiftDoc;
import com.zcy.gift.repository.GiftDocRepository;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author lucker
 * @date 2024/2/15 2:54
 */
@Component
@RocketMQMessageListener(topic = GiftConstant.GIFT_ONSALE_TX_TOPIC, consumerGroup = GiftConstant.GIFT_ONSALE_TX_CONSUMER_GROUP)
public class GiftSpuOnsaleMessageListener implements RocketMQListener<MessageExt> {

    @Autowired
    private GiftDocRepository giftDocRepository;

    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String str = new String(body);
        List<GiftDoc> giftDocs = JSONArray.parseArray(str, GiftDoc.class);
        // 存入es
        giftDocRepository.saveAll(giftDocs);
    }
}
